const router = require('koa-router')();
const {register, login, connectService, getSite, editSite, getUserList, getUserDetail} = require('../service/userService');
const {getScript} = require('../service/scriptService');

router.prefix('/users');

router.get('/', function (ctx, next) {
    ctx.body = 'this is a users response!'
});

router.post('/login', async function (ctx, next) {
    ctx.body = await login(ctx.request.body, ctx);
});

router.post('/register', async function (ctx, next) {
    ctx.body = await register(ctx.request.body, ctx);
});
//网站用户连接客服
router.post('/connectService', async function (ctx, next) {
    ctx.body = await connectService(ctx.request.body);
});
//修改网站设置
router.post('/site', async function (ctx, next) {
    ctx.body = await editSite(ctx.request.body, ctx);
});
//获取网站设置
router.get('/site', async function (ctx, next) {
    ctx.body = await getSite(ctx);
});
//获取用户列表
router.get('/list', async function (ctx, next) {
    ctx.body = await getUserList(ctx);
});
//获取用户详情
router.get('/detail', async function (ctx, next) {
    ctx.body = await getUserDetail(ctx);
});

//获取客服脚本
router.get('/site.js', async function (ctx, next) {
    const script = await getScript(ctx);
    ctx.set('Content-Type', 'text/javascript');
    ctx.body = script;
});

module.exports = router;
